package org.springblade.modules.mes.unit.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.api.ResultCode;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.mes.base.entity.PrdInfo;
import org.springblade.modules.mes.base.service.IPrdInfoService;
import org.springblade.modules.mes.check.insert;
import org.springblade.modules.mes.check.select;
import org.springblade.modules.mes.check.update;
import org.springblade.modules.mes.unit.entity.UnitEntity;
import org.springblade.modules.mes.unit.service.IUnitService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 计量单位
 * @author zhanglianchao
 */
@RestController
@RequestMapping("/unit")
@Api(value = "计量单位", tags = "计量单位")
public class UnitController extends BladeController {

    @Resource
    private IUnitService iUnitService;
    @Resource
    private IPrdInfoService prdInfoService;

    /**
     * 列表
     * @param unitEntity 计量单位
     * @param query 分页信息
     * @return 分页结果
     */
    @GetMapping("/list")
    //@ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页查询", notes = "传入主体及分页信息")
    public R<IPage<UnitEntity>> list(@Validated(select.class) UnitEntity unitEntity, Query query) {
        unitEntity.setTenantId(AuthUtil.getTenantId());
        IPage<UnitEntity> pages = iUnitService.page(Condition.getPage(query),Condition.getQueryWrapper(unitEntity));
        return R.data(pages);
    }

    @GetMapping("/list-none-page")
    //@ApiOperationSupport(order = 1)
    @ApiOperation(value = "无分页查询", notes = "传入主体")
    public R<List<UnitEntity>> listNonePage(@Validated(select.class) UnitEntity unitEntity) {
        unitEntity.setTenantId(AuthUtil.getTenantId());
        List<UnitEntity> pages = iUnitService.list(Condition.getQueryWrapper(unitEntity));
        return R.data(pages);
    }

    @GetMapping("/allList")
    //@ApiOperationSupport(order = 1)
    @ApiOperation(value = "无分页查询去所有")
    public R<List<UnitEntity>> allList() {
        List<UnitEntity> pages = iUnitService.list(new LambdaQueryWrapper<UnitEntity>().eq(UnitEntity::getTenantId,AuthUtil.getTenantId()));
        return R.data(pages);
    }

    /**
     * 新增或修改
     * @param unitEntity 计量单位
     * @return 保存结果
     */
    @PostMapping("/submit")
    //@ApiOperationSupport(order = 2)
    @ApiOperation(value = "新增或修改", notes = "传入主体,修改需传id,新增不需要")
    public R save(@RequestBody @Validated({insert.class, update.class}) UnitEntity unitEntity) {
        String userName = getUser().getUserName();
        //编码唯一性校验
        if(iUnitService.countByCode(unitEntity.getCode(),unitEntity.getId())){
            return R.fail(ResultCode.PARAM_VALID_ERROR,"单位编码已存在！");
        }
        if(StringUtil.isBlank(unitEntity.getCreateUserName())){
            unitEntity.setCreateUserName(userName);
        }
        unitEntity.setUpdateUserName(userName);
        return R.status(iUnitService.saveOrUpdate(unitEntity));
    }

    /**
     * 根据id获取计量单位详情
     * @param id 计量单位id
     * @return 详情
     */
    @GetMapping("/info")
    //@ApiOperationSupport(order = 3)
    @ApiOperation(value = "计量单位详情", notes = "计量单位id")
    public R<UnitEntity> getInfoById(@ApiParam(value = "计量单位id", required = true)@RequestParam("id") Long id){
        return R.data(iUnitService.getById(id));
    }

    /**
     * 逻辑删除
     * @param ids id集合
     * @return 删除结果
     */
    @PostMapping("/remove")
    //@ApiOperationSupport(order = 4)
    @ApiOperation(value = "逻辑删除", notes = "传入主键id集合逗号隔开")
    public R batchRemove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        List<Long> idList = Func.toLongList(ids);
        List<UnitEntity> unitList = iUnitService.listByIds(idList);
        QueryWrapper<PrdInfo> queryWrapper=new QueryWrapper<>();
        for (UnitEntity unit : unitList) {
            queryWrapper.clear();
            queryWrapper.lambda().eq(PrdInfo::getUnit,unit.getCode());
            List<PrdInfo> list = prdInfoService.list(queryWrapper);
            if(CollectionUtil.isNotEmpty(list)){
                return R.fail("单位在产品主数据中被引用,不能删除");
            }
        }
        for (Long id : idList) {

        }
        return R.status(iUnitService.deleteLogic(Func.toLongList(ids)));
    }

    /**
     * 设置默认单位
     * @param id 计量单位id
     * @return 设置结果
     */
    @PostMapping("/default")
    //@ApiOperationSupport(order = 5)
    @ApiOperation(value ="设置为默认单位",notes = "设置为默认单位")
    public R updateDefault(@RequestParam("id") Long id){
        return R.status(iUnitService.setDefault(id));
    }


    @GetMapping("/infoByCode")
    //@ApiOperationSupport(order = 3)
    @ApiOperation(value = "单位详情按code", notes = "计量单位code")
    public R<UnitEntity> getInfoByCode(@ApiParam(value = "计量单位code", required = true)@RequestParam("code") String code){
        return R.data(iUnitService.getByCode(code));
    }
}
